import { _decorator, Component, Event, Node, randomRangeInt } from 'cc';
import { GameDataManager } from '../GameDataManager';
import { UIManager } from 'db://assets/Scripts/Framework/Managers/UIManager';
import { MainUI } from './MainUI';
import { ProjectEvent, ProjectEventManager } from 'db://assets/Scripts/Framework/Managers/ProjectEventManager';
const { ccclass, property } = _decorator;

@ccclass('ChestUI')
export class ChestUI extends Component {

    start() {

    }

    update(deltaTime: number) {

    }

    protected onEnable(): void {
        ProjectEventManager.emit(ProjectEvent.弹出窗口);
    }

    Back() {
        this.node.active = false;
        MainUI.Instance.PlayClick();
    }

    Gacha(event: Event) {
        let target: Node = event.target;
        let num = target.getSiblingIndex();
        switch (num) {
            case 0:
                this.GachaCoin(1);
                break;
            case 1:
                this.GachaCoin(10);
                break;
            case 2:
                this.GachaDiamond(1);
                break;
            case 3:
                this.GachaDiamond(10);
                break;
        }
        MainUI.Instance.PlayClick();
    }

    GachaCoin(count: number) {
        let coins = GameDataManager.Instance.getNumberData("Coin");
        let diamond = GameDataManager.Instance.getNumberData("Diamond");
        let pieces = GameDataManager.Instance.getArrayData<number[]>("SkinPieces");
        let adorns = MainUI.Instance.items.Adorn;
        if (coins < count * 100) return UIManager.ShowTip("金币不足！");
        coins -= count * 100;
        GameDataManager.Instance.setNumberData("Coin", coins);
        let items: number[][] = [];
        for (let i = 0; i < count; i++) {
            const element = [randomRangeInt(0, 3)];
            switch (element[0]) {
                case 0:
                    coins += 50;
                    break;
                case 1:
                    diamond += 25;
                    break;
                case 2:
                    element.push(randomRangeInt(0, adorns.length));
                    element.push(randomRangeInt(0, adorns[element[1]].length));
                    pieces[element[1]][element[2]]++;
                    break;
            }
            items.push(element);
        }
        GameDataManager.Instance.setNumberData("Coin", coins);
        GameDataManager.Instance.setNumberData("Diamond", diamond);
        GameDataManager.Instance.setArrayData("SkinPieces", pieces);
        MainUI.Instance._InitToken();
        MainUI.Instance.ShowFinish2(items);
    }

    GachaDiamond(count: number) {
        let coins = GameDataManager.Instance.getNumberData("Coin");
        let diamond = GameDataManager.Instance.getNumberData("Diamond");
        let pieces = GameDataManager.Instance.getArrayData<number[]>("SkinPieces");
        let adorns = MainUI.Instance.items.Adorn;
        if (diamond < count * 50) return UIManager.ShowTip("钻石不足！");
        diamond -= count * 50;
        GameDataManager.Instance.setNumberData("Diamond", diamond);
        let items: number[][] = [];
        for (let i = 0; i < count; i++) {
            const element = [randomRangeInt(0, 3)];
            switch (element[0]) {
                case 0:
                    coins += 50;
                    break;
                case 1:
                    diamond += 25;
                    break;
                case 2:
                    element.push(randomRangeInt(0, adorns.length));
                    element.push(randomRangeInt(0, adorns[element[1]].length));
                    pieces[element[1]][element[2]]++;
                    break;
            }
            items.push(element);
        }
        GameDataManager.Instance.setNumberData("Coin", coins);
        GameDataManager.Instance.setNumberData("Diamond", diamond);
        GameDataManager.Instance.setArrayData("SkinPieces", pieces);
        MainUI.Instance._InitToken();
        MainUI.Instance.ShowFinish2(items);
    }

}


